using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using TMS.Application.Common.Interface;
using TMS.Application.Utils;

namespace TMS.Api.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class UploadController: ControllerBase
    {

        private readonly IFileUploadService _fileUploadService;

        public UploadController(IFileUploadService fileUploadService)
        {
           _fileUploadService=fileUploadService;
        }


        [HttpPost]
        [Route("/files")]
        public async Task<string> OnPostUploadAsync(IFormCollection files)
        {
            var list = await _fileUploadService.UploadFile(files);
            if (list.Count() > 0)
            {
                return new
                {
                    Code = 1000,
                    Msg = "上传成功",
                    Data = list//变成Guid
                }.SerializeObject();
            }
            else
            {
                return new
                {
                    Code = 4000,
                    Msg = "上传失败，文件扩展名不被允许、文件大小不被允许",
                    Data = list
                }.SerializeObject();
            }
        }


        [HttpGet]
        [Route("/files/{id}")]
        public async Task<FileContentResult> GetFile(Guid id)
        {
            var result = await _fileUploadService.GetFileById(id);
            return result;
        }

    }
}